// Stroke.cpp: implementation of the CStroke class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Scribble.h"
#include "Stroke.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
IMPLEMENT_SERIAL(CStroke,CObject,1)
CStroke::CStroke(UINT nPenWidth)
{
	m_nPenWidth=nPenWidth;
	m_rectBounding.SetRectEmpty();
}
CStroke::CStroke()
{
}
void CStroke::Serialize(CArchive&ar)
{
	if(ar.IsStoring())
	{
		ar<<(WORD)m_nPenWidth;
		ar<<m_rectBounding;
		m_pointArray.Serialize(ar);
	}
	else
	{
		WORD w;
		ar>>w;
		ar>>m_rectBounding;
		m_nPenWidth=w;
		m_pointArray.Serialize(ar);
	}
}
BOOL CStroke::DrawStroke(CDC*pDC)
{
	CPen penStroke;
	if(!penStroke.CreatePen(PS_SOLID,m_nPenWidth,RGB(0,0,0)))
		return FALSE;
	CPen *pOldPen=pDC->SelectObject(&penStroke);
	pDC->MoveTo(m_pointArray[0]);
	for(int i=0;i<m_pointArray.GetSize();i++)
		pDC->LineTo(m_pointArray[i]);
	pDC->SelectObject(pOldPen);
	return TRUE;
}
CStroke::~CStroke()
{

}
#ifdef _DEBUG
/*void CStroke::Dump(CDumpContext &dc)
{
	dc<<m_pointArray;
	TRACE("this stroke width is %d\n",m_nPenWidth);
}*/
#endif
void CStroke::FinishStroke()
{
	if(m_pointArray.GetSize()==0)
	{
		m_rectBounding.SetRectEmpty();
		return;
	}
	CPoint pt=m_pointArray[0];
	m_rectBounding=CRect(pt.x,pt.y,pt.x,pt.y);
	for( int i=1;i<m_pointArray.GetSize();i++)
	{
		pt=m_pointArray[i];
		m_rectBounding.left=min(m_rectBounding.left,pt.x);
		m_rectBounding.right=max(m_rectBounding.right,pt.x);
		m_rectBounding.top=min(m_rectBounding.top,pt.y);
		m_rectBounding.bottom=max(m_rectBounding.bottom,pt.y);
	}
	m_rectBounding.InflateRect(CSize(m_nPenWidth,m_nPenWidth));
	return;
}